<template>
	<div>
		<group>
			<x-input title="手机号" name="mobile" v-model="form.phone" placeholder="请输入手机号码" keyboard="number" is-type="china-mobile"></x-input>
			<x-input title="验证码" class="weui-vcode" placeholder="请输入验证码" v-model="form.code">
				<x-button v-if="enter_code == 0" slot="right" @click.native="get_code" type="primary" mini>发送验证码</x-button>
				<x-button v-else slot="right" type="primary" disabled mini>{{enter_code}}秒后重发</x-button>
			</x-input>
		</group>
		<box gap="20px 10px">
			<x-button type="primary" :show-loading="loading" @click.native="submit">登录</x-button>
			<p style="text-align:center;color:#409EFF;font-size:12px; padding:10px 0;"><span @click="$router.push({name:'register'})">还没有账号? 去注册</span></p>
			<!--<p style="text-align:center;color:#409EFF;font-size:12px; padding:10px 0;">-->
        <!--<span @click="$router.push({name:'updatepwd'})">密码找回</span>-->
      <!--</p>-->
		</box>
	</div>
</template>

<script>
	import axios from 'axios';
	import allapi from '@/http';
	import cookie from 'js-cookie';
	export default {
		components:{

		},
		data(){
			return {
				enter_code : 0,

				loading : false,
				form : {
					phone : '',
					code : ''
				}
			}
		},
		methods : {
			get_code(){
				var time;
				if(this.form.phone == ''){
					return this.$message({
						message: '手机号码不能为空',
						type: 'error'
					});
				}
				// https://setting.api.lanaoboxiang.com/api/sms/send/register/18328308327
				this.$http.setting.post('/sms/send/register/' + this.form.phone,{}).then(res => {
					if(!res.data.code){
						this.$message({
							message: '发送成功,请留意手机短信!',
							type: 'success'
						});
						this.enter_code = 60;
						time = setInterval(()=>{
							this.enter_code --;
							if(this.enter_code == 0){
								clearInterval(time)
							}
						},1000)
					}else{
						this.$message({
							message: res.data.message,
							type: 'error'
						});
					}
				});
			},
			submit(){
				if(this.form.phone == ''){
					return this.$message({
						message: '手机号码不能为空',
						type: 'error'
					});
				}
				if(this.form.code == ''){
					return this.$message({
						message: '请填写验证码',
						type: 'error'
					});
				}
				this.$http.passport.get('vip/'+cookie.get('tanantId')+'/login',{
					params : {
						mobile : this.form.phone,
						smscode : this.form.code
					}
				}).then(res => {
					if(!res.data.code){
						this.$message({
							message: '登录成功',
							type: 'success'
						});
						var u =JSON.stringify(res.data.data.user)
						localStorage.setItem('user', u);
						localStorage.setItem('company', res.data.data.company);
						localStorage.setItem('api_token', res.data.data.api_token);
						localStorage.setItem('refresh_token', res.data.data.refresh_token);
						allapi.forEach(item => {
							item.defaults.headers.common['Authorization'] = 'Bearer ' + res.data.data.api_token;
						})
            if(localStorage.getItem('router')=='usersign'){
              this.$router.push({
                name: 'usersign'
              });
            } else {
              this.$router.push({
                name: 'user_index'
              });
            }

					}else{
						// this.$message({
						// 	message: '登录失败!'+res.data.message,
						// 	type: 'error'
						// })
            this.$message({
              message: '该账号未注册',
              type: 'error'
            })
					}
				})
			}
		},
		computed : {

		},
    mounted(){
		  // console.log(1)
      // var user = localStorage.getItem('user');
      // user= JSON.parse(user)
      // if(user){
      //   this.$router.push({
      //     name: 'user_index'
      //   });
      // }
    },
		created(){

		},
		watch : {

		}
	}
</script>
